﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.IO;
using System.Web;
using System.Web.Services;
using System.Web.UI.WebControls;

namespace wdss.WebServices
{
    /// <summary>
    /// Summary description for DocListService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    //[ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class DocListService : System.Web.Services.WebService
    {

        public DataSet DocumentListService(int userid)
        {
            String result = "Failure.";

            SqlConnection connect = new SqlConnection(ConfigurationManager.ConnectionStrings["aspnetdbConnectionString"].ConnectionString);
            SqlCommand doclist = new SqlCommand("sp_ListDocumentByUserID", connect);
            doclist.CommandType = CommandType.StoredProcedure;
            doclist.Parameters.Add(new SqlParameter("@par_userid", SqlDbType.Int)).Value = userid;

            DataSet ds = new DataSet();

            SqlDataAdapter da = new SqlDataAdapter();
            try
            {
                connect.Open();

                da.SelectCommand = doclist;
                da.Fill(ds);
                connect.Close();

                result = "Success.";
            }
            catch (SqlException)
            {
                result = "Failed to delete document.";
            }
            if (da != null)
            {

            }

            return ds;
        }

        [WebMethod]
        public DataSet DocumentListShareOnService(int userid)
        {
            String result = "Failure.";

            SqlConnection connect = new SqlConnection(ConfigurationManager.ConnectionStrings["aspnetdbConnectionString"].ConnectionString);
            SqlCommand doclist = new SqlCommand("sp_GetShareOptions", connect);
            doclist.CommandType = CommandType.StoredProcedure;
            doclist.Parameters.Add(new SqlParameter("@par_userid", SqlDbType.Int)).Value = userid;

            DataSet ds = new DataSet();

            SqlDataAdapter da = new SqlDataAdapter();
            try
            {
                connect.Open();

                da.SelectCommand = doclist;
                da.Fill(ds);
                connect.Close();

                result = "Success.";
            }
            catch (SqlException)
            {
                result = "Failed to delete document.";
            }
            if (da != null)
            {

            }

            return ds;
        }

        [WebMethod]
        public int DocumentListData(int userid, string doc_title)
        {

            SqlConnection connect = new SqlConnection(ConfigurationManager.ConnectionStrings["aspnetdbConnectionString"].ConnectionString);
            SqlCommand doclist = new SqlCommand("sp_GetDocID", connect);
            doclist.CommandType = CommandType.StoredProcedure;
            doclist.Parameters.Add(new SqlParameter("@par_userid", SqlDbType.Int)).Value = userid;
            doclist.Parameters.Add(new SqlParameter("@par_doc_title", SqlDbType.NChar)).Value = doc_title;

            DataTable dataTableDocument = new DataTable();

            SqlDataAdapter da = new SqlDataAdapter();
            int docId = 0;
            try
            {
                connect.Open();
                da.SelectCommand = doclist;
                da.Fill(dataTableDocument);

                if (dataTableDocument.Rows.Count == 1)
                {
                    docId = (int)dataTableDocument.Rows[0]["doc_id"];
                }

                connect.Close();
            }
            catch (SqlException)
            {
                Server.Transfer("~/GUI/Login.aspx");
            }


            return docId;
        }

        [WebMethod]
        public DataSet CheckedDocumentListData(int userid)
        {
            String result = "Failure.";

            SqlConnection connect = new SqlConnection(ConfigurationManager.ConnectionStrings["aspnetdbConnectionString"].ConnectionString);
            SqlCommand doclist = new SqlCommand("select d.doc_id, d.doc_title, d.doc_create_time, d.doc_last_access, d.doc_type,user1.user1_name from document d,doc_checked, user1 where d.doc_id = doc_checked.doc_id and doc_checked.checked = 1 and doc_checked.user1_id = user1.user1_id", connect);

            DataSet ds = new DataSet();

            SqlDataAdapter da = new SqlDataAdapter();
            try
            {
                connect.Open();
                da.SelectCommand = doclist;
                da.Fill(ds);
                connect.Close();

                result = "Success.";
            }
            catch (SqlException)
            {
                result = "Failed to load checked document.";
            }
            if (da != null)
            {

            }

            return ds;
        }
    }
}
